package com.hellpro.lolbox.home.hero.herolist;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.LolBaseActivity;
import com.hellpro.lolbox.bean.HeroInfo;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.home.HomeJsonUtils;
import com.hellpro.lolbox.utils.OkHttpClientUtils;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;

public class Hero_list extends LolBaseActivity {

    @BindView(R.id.hero_name)
    TextView heroName;
    @BindView(R.id.hero_name2)
    TextView heroName2;

    @BindView(R.id.hero_logo1)
    SimpleDraweeView heroLogo1;
    @BindView(R.id.hero_logo2)
    SimpleDraweeView heroLogo2;
    @BindView(R.id.hero_logo3)
    SimpleDraweeView heroLogo3;
    @BindView(R.id.hero_logo4)
    SimpleDraweeView heroLogo4;
    @BindView(R.id.hero_logo5)
    SimpleDraweeView heroLogo5;
    @BindView(R.id.hero_middle)
    TextView heroMiddle;
    @BindView(R.id.hero_tips)
    TextView heroTips;
    @BindView(R.id.hero_opponentTips)
    TextView heroOpponentTips;
    @BindView(R.id.hero_list_logo)
    SimpleDraweeView heroListLogo;


    private HeroInfo mHeroinfro;
    private String name;

    @Override
    public int getLayoutId() {
        return R.layout.activity_hero_list;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 17) {
                mHeroinfro = (HeroInfo) msg.obj;
                setData();
            }
        }
    };

    @Override
    public void initView() {
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        new Thread(new Runnable() {
            @Override
            public void run() {
                String path = UrlConfigs.HEROINRO + name;
                try {
                    String json = OkHttpClientUtils.getStringFromURL(Hero_list.this, path, null);
                    String jinx = json.replaceAll(name, "Jinx");
                    HeroInfo data = HomeJsonUtils.getData(jinx, HeroInfo.class);
                    Message obtain = Message.obtain();
                    obtain.what = 17;
                    obtain.obj = data;
                    mHandler.sendMessage(obtain);


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }


    //给控件赋值
    private void setData() {
        //头像
        String herologo = UrlConfigs.HEROLOGO + name + "_120x120.jpg";
        Uri parse = Uri.parse(herologo);
        heroListLogo.setImageURI(parse);
        String displayName = mHeroinfro.getDisplayName();
        heroName.setText(displayName);

        String ratingAttack = mHeroinfro.getRatingAttack();
        String ratingDefense = mHeroinfro.getRatingDefense();
        String ratingMagic = mHeroinfro.getRatingMagic();
        String ratingDifficulty = mHeroinfro.getRatingDifficulty();
        String rating = "攻：" + ratingAttack + "防：" + ratingDefense + "法：" + ratingMagic + "难：" + ratingDifficulty;

        heroName2.setText(rating);


        String logo1 = UrlConfigs.HEOATTLOGO + name + "_B_64x64.png";
        Uri parse1 = Uri.parse(logo1);
        heroLogo1.setImageURI(parse1);

        String logo2 = UrlConfigs.HEOATTLOGO + name + "_Q_64x64.png";
        Uri parse2 = Uri.parse(logo2);
        heroLogo2.setImageURI(parse2);
        String logo3 = UrlConfigs.HEOATTLOGO + name + "_W_64x64.png";
        Uri parse3 = Uri.parse(logo3);
        heroLogo3.setImageURI(parse3);
        String logo4 = UrlConfigs.HEOATTLOGO + name + "_E_64x64.png";
        Uri parse4 = Uri.parse(logo4);
        heroLogo4.setImageURI(parse4);
        String logo5 = UrlConfigs.HEOATTLOGO + name + "_R_64x64.png";
        Uri parse5 = Uri.parse(logo5);
        heroLogo5.setImageURI(parse5);
        String content = mHeroinfro.getJinx_B().getDescription() + mHeroinfro.getJinx_B().getEffect();
        heroMiddle.setText(content);

        String tips = mHeroinfro.getTips();
        heroTips.setText(tips);

        String opponentTips = mHeroinfro.getOpponentTips();
        heroOpponentTips.setText(opponentTips);

    }




    @OnClick({R.id.hero_logo1, R.id.hero_logo2, R.id.hero_logo3, R.id.hero_logo4, R.id.hero_logo5})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.hero_logo1:
                String content = mHeroinfro.getJinx_B().getDescription() + mHeroinfro.getJinx_B().getEffect();
                heroMiddle.setText(content);
                break;
            case R.id.hero_logo2:
                String content2 = mHeroinfro.getJinx_B().getDescription() + mHeroinfro.getJinx_Q().getEffect();
                heroMiddle.setText(content2);
                break;
            case R.id.hero_logo3:
                String content3 = mHeroinfro.getJinx_B().getDescription() + mHeroinfro.getJinx_W().getEffect();
                heroMiddle.setText(content3);
                break;
            case R.id.hero_logo4:
                String content4 = mHeroinfro.getJinx_B().getDescription() + mHeroinfro.getJinx_E().getEffect();
                heroMiddle.setText(content4);
                break;
            case R.id.hero_logo5:
                String content5 = mHeroinfro.getJinx_B().getDescription() + mHeroinfro.getJinx_R().getEffect();
                heroMiddle.setText(content5);
                break;
        }

    }
}

